package com.example.demo.util;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * BCrypt 密码哈希生成器
 * 用于生成和验证 BCrypt 哈希值
 * 
 * @author zhang
 * @date 2024
 */
public class BCryptHashGenerator {
    
    public static void main(String[] args) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        
        // 要加密的密码列表
        String[] passwords = {
            "123456",
            "admin123",
            "password",
            "test123"
        };
        
        System.out.println("========================================");
        System.out.println("BCrypt 密码哈希生成器");
        System.out.println("========================================");
        System.out.println();
        
        // 生成哈希值
        for (String password : passwords) {
            String hash = encoder.encode(password);
            System.out.println("密码: " + password);
            System.out.println("哈希值: " + hash);
            System.out.println();
        }
        
        System.out.println("========================================");
        System.out.println("验证示例");
        System.out.println("========================================");
        System.out.println();
        
        // 验证示例：验证 "123456" 是否匹配数据库中的哈希
        String databaseHash = "$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iKYvNp7/txG0HftoLDx.uY4L4vZK";
        String testPassword = "123456";
        boolean matches = encoder.matches(testPassword, databaseHash);
        System.out.println("测试密码: " + testPassword);
        System.out.println("数据库哈希: " + databaseHash);
        System.out.println("匹配结果: " + (matches ? "✓ 匹配" : "✗ 不匹配"));
        System.out.println();
        
        // 验证错误密码
        String wrongPassword = "wrong123";
        boolean wrongMatches = encoder.matches(wrongPassword, databaseHash);
        System.out.println("测试密码: " + wrongPassword);
        System.out.println("匹配结果: " + (wrongMatches ? "✓ 匹配" : "✗ 不匹配（预期结果）"));
    }
}

